package com.rss.provider.spring.rest;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rss.provider.constant.NavigationConstant;
import com.rss.provider.service.RssService;
import com.rss.provider.spring.form.SubscriberForm;
import com.rss.provider.spring.form.UpdateForm;
import com.rss.provider.spring.rest.wrapper.SubscriberListWrapper;
import com.rss.provider.spring.rest.wrapper.UpdateListWrapper;
import com.rss.provider.util.DateUtils;

@Controller
@RequestMapping(value="/rest")
public class RssProviderRestController {
	
	@Autowired
	@Qualifier("RssServiceImpl")
	private RssService rssService;
	
	@RequestMapping(value="DSsubscriber.htm", method = RequestMethod.POST, headers = "Accept=text/html, application/json,application/xml",
			consumes = {"application/xml","application/json"})
	public @ResponseBody String subscribe(@RequestBody SubscriberForm subscriberForm){
		String result = rssService.addSubscriber(subscriberForm);
		if (result.equals("success"))
			return NavigationConstant.SUCCESS;
		return NavigationConstant.FAILED;
	}
	
	@RequestMapping(value="DSunsubscriber.htm", method = RequestMethod.DELETE, headers = "Accept=text/html, application/json,application/xml",
			consumes = {"application/xml","application/json"})
	public @ResponseBody String unSubscribe(@RequestBody SubscriberForm unsubscriberForm) {
		String result = rssService.removeSubscriber(unsubscriberForm);
		if (result.equals("success"))
			return NavigationConstant.SUCCESS;
		return NavigationConstant.FAILED;
	}
	
	@RequestMapping(value = "DSsubscribers.htm", method = RequestMethod.GET, headers = "Accept=text/html, application/json,application/xml",
			produces = {"application/xml","application/json"})
	public @ResponseBody
	SubscriberListWrapper showSubscribers(@RequestParam("subscribedTo") String subscribedTo) {
		List<SubscriberForm> subscriberForms = rssService.getAllSubscribers(subscribedTo);
		SubscriberListWrapper subscriberListWrapper = new SubscriberListWrapper();
		subscriberListWrapper.setCustomerForms(subscriberForms);
		return subscriberListWrapper;
	}

	@RequestMapping(value = "DSupdates.htm", method = RequestMethod.GET, headers = "Accept=text/html, application/json,application/xml",
			produces = {"application/xml","application/json"})
	public @ResponseBody
	UpdateListWrapper showUpdates(@RequestParam("fromDate") String fromDate) {
		List<UpdateForm> updateForms = rssService.getUpdates(DateUtils.convertStringIntoDateYYMMDD(fromDate));
		UpdateListWrapper updateListWrapper = new UpdateListWrapper();
		updateListWrapper.setUpdateForms(updateForms);
		return updateListWrapper;
	}
}
